package br.com.lumis.ims.service.cloud.dataprovider;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableDataProvider;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;
import lumis.util.PortalUtil;
import br.com.lumis.ims.model.Metrica;
import br.com.lumis.ims.model.MetricaInstancia;
import br.com.lumis.ims.service.CloudWatchService;
import br.com.lumis.ims.service.cloud.util.ImsRequestParameters;

import com.amazonaws.auth.AWSCredentials;

public class MetricaDataProvider  extends TableDataProvider {

	private static final NumberFormat NF = NumberFormat.getNumberInstance();
	public MetricaDataProvider() throws PortalException {
		super();		
	}
	
	
	@Override
	public void loadData(SessionConfig sessionConfig, TableSource source,ITransaction transaction) throws PortalException {
		
		try{
			String idInstancia = (String)source.getDouiContext().getRequest().getParameter(ImsRequestParameters.INSTANCIAID);
			Calendar cal = Calendar.getInstance();
			
			cal.add(Calendar.DATE, -14);
			Date inicio = new SimpleDateFormat("yyyyMMddHHmm").parse(getDateEnd(cal));
			
			cal = Calendar.getInstance();
			Date fim = new SimpleDateFormat("yyyyMMddHHmm").parse(getDateEnd(cal));
			
			AWSCredentials credentials = ImsRequestParameters.getCredentials(sessionConfig, transaction);
			TabularData tabularData = source.getData();
							
			
			Map<MetricaInstancia, Set<Metrica>> metricasInstancia = new CloudWatchService().getMetricasInstancia(credentials, idInstancia, inicio, fim);
			Set<MetricaInstancia> instanceMetrics = new TreeSet<MetricaInstancia>(metricasInstancia.keySet());
			Iterator<MetricaInstancia> itInstanceMetrica = instanceMetrics.iterator();
			while(itInstanceMetrica.hasNext()) {								
				
				MetricaInstancia metrica = itInstanceMetrica.next();
				//System.out.println("\t" + metrica.nomeMetrica());
				Iterator<Metrica> itMetricas = metricasInstancia.get(metrica).iterator();
				while(itMetricas.hasNext()) {
					ISourceData sourceData = tabularData.addRow();
					Metrica data = itMetricas.next();
					
					sourceData.put(ImsRequestParameters.ID, PortalUtil.generateNewGuid());
					sourceData.put(ImsRequestParameters.INSTANCIAID, idInstancia);								
					sourceData.put(ImsRequestParameters.NOME, metrica.name());
					sourceData.put(ImsRequestParameters.METRICA, metrica.nomeMetrica());
					
					if(metrica.name().equals("CPU_UTILIZATION"))
						sourceData.put(ImsRequestParameters.VALOR, NF.format(data.getValor()));
					else
						sourceData.put(ImsRequestParameters.VALOR, data.getValor());
					
					sourceData.put(ImsRequestParameters.TIMESTAMP, data.getTimestamp());
										
					//System.out.println("\t\t" + data.getTimestamp() + "\t" + NF.format(data.getValor()));
				}
			}
		} catch (ParseException e) {		
			e.printStackTrace();
		}
		
							
	}
	
	private String getDateEnd(Calendar cal){
		
		String ano = String.valueOf(cal.get(Calendar.YEAR));
		String mes = cal.get(Calendar.MONTH) < 10 ? "0"+(cal.get(Calendar.MONTH)+1) : String.valueOf(cal.get(Calendar.MONTH)+1);
		String dia = cal.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + (cal.get(Calendar.DAY_OF_MONTH)+1) : String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		String hora = cal.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + (cal.get(Calendar.HOUR_OF_DAY)) : String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		String minuto = "00";//cal.get(Calendar.MINUTE) < 10 ? "0" + (cal.get(Calendar.MINUTE)) : String.valueOf(cal.get(Calendar.MINUTE));
		 		
		return  ano+mes+dia+hora+minuto;
	}
	
}
